package com.example.client.ui;
import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

class DragMouseAdapter extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
                JComponent c = (JComponent) e.getSource();
                TransferHandler handler = c.getTransferHandler();
                handler.exportAsDrag(c, e, TransferHandler.COPY);
        }
}

public class test1 {
        public static void main(String[] args) {
                JFrame f = new JFrame("Drag & Drop");
                ImageIcon icon1 = new ImageIcon("C:\\image\\ball.jpg");
                ImageIcon icon2 = new ImageIcon("C:\\image\\ball.jpg");
                JPanel panel = new JPanel(new GridLayout(2, 1));
                JPanel panel1 = new JPanel(new BorderLayout());
                JPanel panel2 = new JPanel();
                panel1.setBackground(Color.red);
                panel2.setBackground(Color.white);
                final JLabel label1 = new JLabel(icon1);
                JLabel label2 = new JLabel(icon2);
                MouseListener listener = new DragMouseAdapter();
                label1.addMouseListener(listener);
                label2.addMouseListener(listener);
                
                label1.addMouseMotionListener(new MouseMotionListener() {
					
					private Rectangle r;

					@Override
					public void mouseMoved(MouseEvent e) {
						Point locationOnScreen = e.getLocationOnScreen();
						r = new Rectangle(locationOnScreen.x,locationOnScreen.y,label1.getWidth(), label1.getHeight());
						label1.setBounds(r);
						label1.repaint();
						label1.revalidate();
						System.out.println("Inisde mouse motion listenre..");
						
						
					}
					
					@Override
					public void mouseDragged(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
				});
                label2.addMouseListener(listener);
                
                label1.setTransferHandler(new TransferHandler("icon"));
                label2.setTransferHandler(new TransferHandler("icon"));
                panel1.add(label1);
                panel2.add(label2);
                panel.add(panel1);
                panel.add(panel2);
                f.setLayout(new GridLayout(1, 2));
                f.setSize(300, 400);
                f.add(panel);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
        }
}